
proc Convert {cInPcapFile cOutPcapFile nOffset nTail {lHeader "00 0a 9d 08 f6 3c 6c f0 49 51 73 73 08 00"} } {
    
    set cDestFile "K12Temp.txt"
    set cHexFile "Temp.txt"
    set aPackets(0) ""
    
    Pcap2K12 $cInPcapFile $cDestFile
    set nNumPackets [ReadK12 $cDestFile]
    WriteHex $cHexFile $nNumPackets $nOffset $nTail $lHeader
    Hex2Pcap $cHexFile $cOutPcapFile
}


proc Pcap2K12 {cPcapFile cDestFile} {

    exec editcap -T ether -F k12text $cPcapFile $cDestFile
    
    return
    
}

proc Hex2Pcap {cInFile cOutFile} {
    
    exec Text2pcap $cInFile $cOutFile 
}



proc ReadK12 {cFile} {
    
    upvar aPackets aPackets
    
    set hFileIn [open $cFile "r"]
    
    set nNumPackets 0
    while {! [eof $hFileIn]} {
        gets $hFileIn cLine
        if {[string first "|0" $cLine] == 0} {
            incr nNumPackets
            set nStart [expr [string first "|" $cLine 1] + 1] ; # find the second pipe and move forward one for the first byte.
            set nEnd [expr [string last "|" $cLine] -1] ;       # eliminate the last pipe to avoid having an extra item in the list
            set cLine [string range $cLine $nStart $nEnd]
            set aPackets($nNumPackets) [split $cLine "|"]
        }
    }
    
    close $hFileIn
    return $nNumPackets
}

proc WriteHex {cFile nNumPackets nOffset nTail lHeader} {
    
    upvar aPackets aPackets
    
#    set nOffset [expr $nOffset -1] ; # Account for the fact that lists start with an offset of 0
    set hFileOut [open $cFile "w"]
    set lOffset [list "0000"]
    
    for {set i 1} {$i <= $nNumPackets} {incr i} {
        set lLine [lrange $aPackets($i) $nOffset end-$nTail]
        puts $hFileOut [concat $lOffset $lHeader $lLine]
    }
    close $hFileOut
    
}